package com.kfm.dao;

import com.kfm.model.SEmp;
import com.kfm.util.LoadDatasource;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 对于 一张表 的增删改查
 *
 */
public class SEmpDao {
    private QueryRunner queryRunner;
    private BasicRowProcessor basicRowProcessor;
    public SEmpDao() {
        try {
            queryRunner = new QueryRunner(LoadDatasource.getDatasource());
            basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<SEmp> selectAll() throws Exception {
        String sql = "select * from s_emp";
        return queryRunner.query(sql,new BeanListHandler<>(SEmp.class, basicRowProcessor));
    }

    public int deleteById(Integer id) throws SQLException {
        String sql = "delete from s_emp where id = ?";
        return queryRunner.update(sql, id);
    }

    public SEmp selectById(Integer id) throws SQLException {
        String sql = "select * from s_emp where id = ?";
        return queryRunner.query(sql, new BeanHandler<>(SEmp.class, basicRowProcessor), id);
    }
}
